// import { header_img_xianshi, sun_download_hidden } from './code_loginit'

//登录post请求

const myAxios_third = axios.create({
  baseURL: 'http://d67ekh.natappfree.cc',
  timeout: 30000,
  headers: {
    'Content-Type': 'application/json'
  }
})
myAxios.defaults.withCredentials = true
async function userLogin(hyj) {
  try {
    const response = await myAxios_third.post('/user/login', {
      email: hyj.email?.trim(),
      password: hyj.password
    })
    return {
      code: response.data.code,
      data: response.data.data,
      msg: response.data.msg
    }
  }
  catch (error) {
    const status = error.response?.status
    return {
      code: status,
      data: null,
      msg: error.message
    }
  }
}


const account2 = document.querySelector('.in_middle .in_email .account')
const password2 = document.querySelector('.in_middle .import .password')
const email_wrong_up = document.querySelector('.in_middle .email_incomplete .wrong')
const email_empty_up = document.querySelector('.in_middle .email_incomplete .empty')
const email_wrong_down = document.querySelector('.in_middle .password_incomplete .wrong')
const email_empty_down = document.querySelector('.in_middle .password_incomplete .empty')
const p_symbol_up = document.querySelector('.in_middle .email_incomplete i')
const p_symbol_down = document.querySelector('.in_middle .password_incomplete i')
const p_log = document.querySelector('.in_middle .in_log .log_log_log')
const header_img_hidden = document.querySelector('.header_img img')
const sun_download_xianshi = document.querySelector('.sun_download')
const notfound = document.querySelector('.password_notfound .notfound')
const notfound_i = document.querySelector('.password_notfound i')


//邮箱账号重新输入时，提示信息消失
account2.addEventListener('input', () => {
  email_wrong_up.style.display = 'none'
  p_symbol_up.style.display = 'none'
})
//邮箱账号输入后，提示信息消失
account2.addEventListener('input', () => {
  email_empty_up.style.display = 'none'
  p_symbol_up.style.display = 'none'
})
//密码输入时，提示信息消失
password2.addEventListener('input', () => {
  email_empty_down.style.display = 'none'
  p_symbol_down.style.display = 'none'
})

p_log.addEventListener('click', async () => {
  //邮箱账号为空提示
  if (!account2.value) {
    email_empty_up.style.display = 'inline-block'
    p_symbol_up.style.display = 'inline-block'
  }
  //邮箱账号格式不对提示
  else if (!standard.test(account2.value)) {
    email_wrong_up.style.display = 'inline-block'
    p_symbol_up.style.display = 'inline-block'
  }
  //密码未输入提示
  if (!password2.value) {
    email_empty_down.style.display = 'inline-block'
    p_symbol_down.style.display = 'inline-block'
  }
  //密码格式不对提示
  else if (!passwordRegex.test(password2.value)) {
    email_wrong_down.style.display = 'inline-block'
    p_symbol_down.style.display = 'inline-block'
  }
  if (account2.value && password2.value && standard.test(account2.value) && passwordRegex.test(password2.value)) {
    const value = {
      email: account2.value,
      password: password2.value
    }
    await userLogin(value)
      .then((result) => {
        console.log(result)
        if (result.code === -1) {
          notfound.style.display = 'inline-block'
          notfound_i.style.display = 'inline-block'
        }
        if (result.code === 0) {
          boss.style.display = 'none'
          header_img_hidden.style.display = 'block'
          sun_download_xianshi.style.display = 'none'
        }
      })
      .catch((err) => { console.log(err) })
  }
})

document.querySelector('.header_img').addEventListener('click', () => {
  window.location.reload()
})